var level = '';
var parentId = '';
$(function(){
	/**
	 * 截取参数
	 */
	var url = tl_config.his_url;
	var start =url.indexOf('?')+1;
	var params = url.substring(start);
	var parr = [];
	var paramMap = {
	};
	parr = params.split('&');
	for(var i in parr){
		var key=parr[i].substring(parr[i].indexOf('='),0);
		paramMap[key]=parr[i].substring(parr[i].indexOf('=')+1);
	}
	var id = paramMap['id'];
	//根据id获取数据
	$.ajax({
		type:'post',
		url:server_url+'/rest/organization/getorgls.do?ran='+Math.random()+'&token='+token,
		data:{id:id},
		dataType:'json',
		async:false,
		success:function(data){
			if(data.retCode==="0000"){
				var result = $.parseJSON(data.data);
				$("#org_No").html(result[0].orgNo);
				$("#id").val(result[0].id);
				$("#orgNo").val(result[0].orgNo);
				$("#orgName").val(result[0].orgName);
				$("#orgDesc").val(result[0].orgDesc);
				parentId = result[0].parentId;
				level = result[0].level;
			}else{
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:'系统异常'
					},
					autoClose : 2000
				});
				console.error(data.retMsg);
			}
		},
		error:function(error,request,text){
			easyDialog.open({
				container:{
					header:'温馨提示',
					content:'系统异常'
				},
				autoClose : 2000
			});
			console.error(error);
		}
	});
	//获取上级组织
	if(parentId!='0'){
		$.ajax({
			type:'post',
			data:{id:parentId},
			url:server_url+'/rest/organization/getorgls.do?ran='+Math.random()+'&token='+token,
			dataType:'json',
			success:function(data){
				var result = $.parseJSON(data.data);
				if(data.retCode='0000'){
					$("#parentName").html(result[0].orgName);
				}else{
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:'系统异常！'
						},
						autoClose:2000
					});
					console.error(data.retMsg);
				}
			}
		});
	}else{
		$("#parentName").html("顶级组织");
	}
	$("#oform").validate({
		messages:{
			orgName:"该值不能为空!"
		},
		submitHandler:function(){
			sub();
		},
		errorPlacement: function(error, element) {
		    /*element.next().next().html(error);*/
			element.parent().append(error);
		}
	});
});
function toBack(){
	url = 'orgList.do';
	tl_config.loadToWorkArea(url);
}
function sub(){
	var orgName = $("#orgName").val();
	if(orgName!=''){
		var username=localStorage.getItem("username");
		$("#oform").append("<input type=\"hidden\" name=\"modifyUser\" value=\""+username+"\"/>");
		$.ajax({
			type:'post',
			data:$("#oform").serialize(),
			url:server_url+'rest/organization/editorg.do?ran='+Math.random()+'&token='+token,
			dataType:'json',
			success:function(data){
				if(data.retCode==="0000"){
					toBack();
				}else{
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:'系统异常'
						},
						autoClose : 2000
					});
					console.error(data.retMsg);
				}
			}
		});
	}else{
		easyDialog.open({
			container:{
				header:'温馨提示',
				content:'请输入组织名称！'
			},
			autoClose : 2000
		});
	}
}